<?php

namespace app\api\controller;

use think\Db;
use app\common\validate\UserValidate;

class Login extends ApiBase
{
	
	protected $datas;

	/**
	 * 登录验证
	 * @Author   Yichen
	 * @DateTime 2020-05-04T15:17:38+0800
	 * @return   [type]                   [description]
	 */
	public function LoginCheck()
	{
		$this->datas = $this->params;
		$validate = new UserValidate();
		if (!$validate->scene('userLogin')->check($this->datas)) {
			return json(['code' => 0, 'msg' => $validate->getError(), 'data' => '']);
		}
		$mobile = $this->datas['mobile'];
		$password = md5($this->datas['password']);
		$id = Db::name('t_user')->where('mobile', $mobile)->value('id');
		if (empty($id)) {
			return json(['code' => 0, 'msg' => '该号码尚未注册', 'data' => '']);
		}
		$userData = Db::name('t_user')->where(['mobile' => $mobile, 'password' => $password])->find();
		if (empty($userData)) {
			return json(['code' => 0, 'msg' => '密码错误', 'data' => '']);
		}
		$sessionData = [
			'uid' => $userData['id'],
			'username' => $userData['username'],
			'mobile' => $userData['mobile'],
			'avatar' => $userData['avatar'],
			'email' => $userData['email']
		];
		session('userinfo', $sessionData);
		return json(['code' => 1, 'msg' => '登录成功', 'data' => $sessionData]);
	}

	/**
	 * 注册
	 * @Author   Yichen
	 * @DateTime 2020-05-04T16:51:55+0800
	 * @return   [type]                   [description]
	 */
	public function register()
	{
		$this->datas = $this->params;
		$validate = new UserValidate();
		if (!$validate->scene('register')->check($this->datas)) {
			return json(['code' => 0, 'msg' => $validate->getError(), 'data' => '']);
		}
		$username = $this->datas['username'];
		$mobile = $this->datas['mobile'];
		$password = md5($this->datas['password']);
		$repassword = md5($this->datas['repassword']);
		$email = $this->datas['email'];
		$vcode = $this->datas['vcode'];
		if ($password != $repassword) {
			return json(['code' => 0, 'msg' => '密码与确认密码不一致', 'data' => '']);
		}
		$code = session('code_' . $email);
		$codeUseTime = session('code_time_' . $email);
		if ($vcode != $code) {
			return json(['code' => 0, 'msg' => '验证码错误', 'data' => '']);
		}
		if (time() - $codeUseTime > 300) {
			session('code_' . $email, null);
			session('code_time_' . $email, null);
			return json(['code' => 0, 'msg' => '验证码已过期', 'data' => '']);
		} 
		$data = [
			'username' => $username,
			'password' => $password,
			'mobile' => $mobile,
			'email' => $email,
			'status' => 1,
			'create_time' => time()
		];
		$result = Db::name('t_user')->insert($data);
		if (!$result) {
			return json(['code' => 0, 'msg' => '注册失败', 'data' => '']);
		}
		session('code_' . $email, null);
		session('code_time_' . $email, null);
		return json(['code' => 1, 'msg' => '注册成功', 'data' => '']);
	}

	/**
	 * 发送邮箱验证码
	 * @Author   Yichen
	 * @DateTime 2020-05-04T16:22:52+0800
	 * @return   [type]                   [description]
	 */
	public function sendEmailCode()
	{
		$this->datas = $this->params;
		$validate = new UserValidate();
		if (!$validate->scene('emailCode')->check($this->datas)) {
			return json(['code' => 0, 'msg' => $validate->getError(), 'data' => '']);
		}
		$email = $this->datas['email'];
		$result = $this->getEmailCode($email);
		if (!$result[0]) {
			return json(['code' => 0, 'msg' => $result[1], 'data' => '']);
		}
		return json(['code' => 1, 'msg' => '发送成功', 'data' => '']);
	}


	/**
	 * 微信登录
	 * @Author   Yichen
	 * @DateTime 2020-05-04T21:24:55+0800
	 * @return   [type]                   [description]
	 */
	public function wechatLogin()
	{
		$redirect_uri = "";//回调页面
        $redirect_uri = urlencode($redirect_uri);//该回调需要url编码
        $appid =  "";
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
        $requestUrl = sprintf($url, $appid, $redirect_uri);
        $this->success($requestUrl);
	}
}